package root.electronclass.electronclass.home.widget;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import root.electronclass.electronclass.common.base.BasicAdapter;
import root.electronclass.electronclass.common.base.BindingHolder;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.entity.ClassDto;
import root.electronclass.electronclass.mvp.entity.HomeClassInfoH;
import root.electronclass.electronclass.mvp.rest.RestManager;
import root.electronclass.electronclass.databinding.HomeClassInfoBinding;
import root.electronclass.electronclass.databinding.HomeClassItemBinding;
import root.electronclass.electronclass.other.PictureActivity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeClassView {
    private static final String TAG = "HomeClassView";

    private Context context;
    private HomeClassInfoBinding binding;


    private ArrayList<String> pics = new ArrayList<>();
    private int index = 0;

    private int size = 0;

    public HomeClassView(Context context, ViewGroup view) {
        this.binding = HomeClassInfoBinding.bind(view);
        this.context = context;
        init();
    }

    private void init() {
        binding.tvTitle.setText(GlobalParameter.getEcardInfo().getClassName());
        request();
    }

    private void request() {
        long id = GlobalParameter.getEcardInfo().getClassId();
        long sid = GlobalParameter.getSchoolInfo().getId();
        ClassDto dto = new ClassDto();
        dto.setDepartId(String.valueOf(id));
        dto.setSchoolId(String.valueOf(sid));

        RestManager.getInstance().initRest().homeClassInfo(dto).enqueue(new Callback<>() {
            @Override
            public void onResponse(Call<BaseResponse<List<HomeClassInfoH>>> call, Response<BaseResponse<List<HomeClassInfoH>>> response) {
                if (response.isSuccessful()) {
                    assert response.body() != null;
                    initViewPager(response.body().getRows());
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<HomeClassInfoH>>> call, Throwable t) {

            }
        });
    }

    public void refresh() {
        request();
    }
    private void initViewPager(List<HomeClassInfoH> list) {
        List<View> mViews = new ArrayList<>();
        pics.clear();
        if (list == null) {
            list = new ArrayList<>();
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPicFormat() != null && list.get(i).getPicFormat().size() > 0) {
                for (int j = 0; j < list.get(i).getPicFormat().size(); j++) {
                    pics.add(list.get(i).getPicFormat().get(j).get("url"));
                }
            }
        }


        for (int i = 0; i < list.size(); i += 2) {
            int end = i + 2;
            if (i + 2 > list.size()) {
                end = list.size();
            }
            List<HomeClassInfoH> page = list.subList(i, end);
            binding.viewPager.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL,true));
            final int tempIndex = i;
            BasicAdapter<HomeClassInfoH, HomeClassItemBinding> adapter = new BasicAdapter<HomeClassInfoH, HomeClassItemBinding>() {
                @Override
                public void onBindViewHolder(@NonNull BindingHolder<HomeClassItemBinding> holder, @Nullable HomeClassInfoH item) {

                    String url = "";
                    if (item.getPicFormat() != null && item.getPicFormat().size() > 0) {
                        url = item.getPicFormat().get(0).get("url");
                    }

                    holder.getBinding().tvTitle.setText(item.getName());
                    holder.getBinding().ivPic.setImageURI(url);

                }

                @Override
                public void onBindViewHolder(@NonNull BindingHolder<HomeClassItemBinding> holder, int position) {
                    super.onBindViewHolder(holder, position);
                    final int tempPos = position;
                    holder.getBinding().getRoot().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            PictureActivity.startActivity(context, pics, tempIndex + tempPos);
                        }
                    });
                }
            };
            adapter.replaceAll(page);
            binding.viewPager.setTag(adapter);
            binding.viewPager.setAdapter(adapter);
            binding.viewPager.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mViews.add(binding.viewPager);
        }
    }



}
